<?php

namespace web\common\utils;

use Yansongda\Pay\Pay;
use Yansongda\Pay\Log;

class AliPay{

    protected $config = [
        'app_id' => '2019082866530293',
        'notify_url' => 'http://xx.lg.cool/api/notify/alipayNotify',
        'return_url' => 'http://xx.lg.cool/api/notify/alipayNotify',
        'ali_public_key' => 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA08v6LCBq3gAdIOOPnWei7PRNAEw+lU/xdSWXxEjh09CNINgXN0ha3VHgEtgkjnRfEVq8xk9K4xK1l2kBV3ZTafZ1jPl7qvf57dhuPl+Dq8vK7BBoRWEa0XSts74tbr21t6iYxDYLtI6fjGbpZuKtbil1kLwcfyu8fKff5al29iCvbOkQoTvAakBxTZZcF63C78ufqnQFTp/OC/7xugW0V8hXH2x+ZOs/O9UEck5dvPXQM83QXkgydtGm5+DdDlH7Sj7R41HjCQgUyoSXY8/pSaXTtdMtbKH6VI4dR+ZEHMSnZIcVIoX0b3ivqeArm9EPgsrmPFGKkEe6oIobaLKV9wIDAQAB',
        // 'ali_public_key' => 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAl8AXSZAWQJXiO/jaXp8VXE7yC754NP093EbvjruB5Id4gLboAFeCudLlnTdNPgCKR5YpyVhuJsL6XFx88g57w3j5mtt0Xvap+v2V4ADmWel/kxpLMNq1bS6HO3XlEzbVRaaaiKsOl07Ukbn5ixiUK/KLzmUK7mwySg5IW3i9u2Ctd6Ycq9qOO0hTtFVQYtFs2rQOI8KjPKFRc0eUkK6RGlLkKKJkFYSMBT5k/COkiXl8GOsKQg9vFC+4BAATn48EUMc2fnqs6x6GwlG8SKDSBM9XseS30NAQtRJWv0FRlBq/mnrxzbie7SYSOd5qpemAkTL6uzV/WAbPgdQlypXBzwIDAQAB',
        // 加密方式： **RSA2**  
        'private_key' => 'MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCXwBdJkBZAleI7+NpenxVcTvILvng0/T3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S0mCjQ2ODj9zUrv0FDDLorb6bo9im+d2y69GkMwzXe22g6R6sjlZFHJIYjQ2juwva8gVfGfCj8WGepnV9jzi7WwGxluqMfhmU8vPX+H+26ITKxwKBgQCIIcVgFMG56JZgj91Nq7NmQW1qmrXv2J19ngg7DUt+mM6gjm1N42m3gVEh323qVPLpXvApjYpz0z4mHJDTTg577PT9zHcoxKyv4vCBphT7rSwtfYjAlpc6lDsVBpzsAskJ/GDqzlnNZBnCXhKCI98tLJHgIi0y0J1lbK3fdM5cBQKBgQCzABYB77s4/mV87sI+osVYhBd4c7POnO2iL0hqixXABGn24l5MnhohUTxJ+mCqQvU/zZlfsN3I299gWVHmhfCX9M4dlIDZPXhJqdkdaOOtlytGJyJiTMMop04+wnolfQPQ9qK6PU6BrX8/WIpqDsoOY3hljqS4u2Cx4xGXh+fpFQ==',
        'log' => [ // optional
            'file' => './logs/alipay.log',
            'level' => 'info', // 建议生产环境等级调整为 info，开发环境为 debug
            'type' => 'single', // optional, 可选 daily.
            'max_file' => 30, // optional, 当 type 为 daily 时有效，默认 30 天
        ],
        'http' => [ // optional
            'timeout' => 5.0,
            'connect_timeout' => 5.0,
            // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
        ],
        'mode' => 'normal', // optional,设置此参数，将进入沙箱模式
    ];

    public function topay($data)
    {
        $order = [
            'out_trade_no' => $data['order_code'],
            'total_amount' => $data['amount'],
            'subject' => '实名认证金额',
        ];

        $alipay = Pay::alipay($this->config)->app($order);

        return $alipay->getContent();// laravel 框架中请直接 `return $alipay`
        // return $alipay->send();// laravel 框架中请直接 `return $alipay`
    }

    public function return(){
        $data = Pay::alipay($this->config)->verify(); // 是的，验签就这么简单！

        // 订单号：$data->out_trade_no
        // 支付宝交易号：$data->trade_no
        // 订单总金额：$data->total_amount
    }

    public function notify(){
        $alipay = Pay::alipay($this->config);
        try{
            $data = $alipay->verify(); // 是的，验签就这么简单！
            Log::debug('Alipay notify', $data->all());
            if($data){
                $out_trade_no = $data->out_trade_no;
                $authOrderM = new \addons\member\model\AuthOrder();
                $info = $authOrderM->alias('o')->field('o.*,m.pid,m.phone')->where('order_code',$out_trade_no)->join('member_account m','m.id=o.user_id')->find();
                $res = $authOrderM->where('id',$info['id'])->update(['status'=>1,'update_time'=>date('Y-m-d H:i:s'),'recharge_type'=>'alipay']);
                if($res) return $alipay->success()->send();
                else echo 'false';
            }else{
                echo "false";
                // if($data) return $data->all();
                // else return false;
                // 请自行对 trade_status 进行判断及其它逻辑进行判断，在支付宝的业务通知中，只有交易通知状态为 TRADE_SUCCESS 或 TRADE_FINISHED 时，支付宝才会认定为买家付款成功。
                // 1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号；
                // 2、判断total_amount是否确实为该订单的实际金额（即商户订单创建时的金额）；
                // 3、校验通知中的seller_id（或者seller_email) 是否为out_trade_no这笔单据的对应的操作方（有的时候，一个商户可能有多个seller_id/seller_email）；
                // 4、验证app_id是否为该商户本身。
                // 5、其它业务逻辑情况
            }
        } catch (\Exception $e) {
            echo "error";
            return false;
            // $e->getMessage();
        }

        // return $alipay->success()->send();// laravel 框架中请直接 `return $alipay->success()`
    }
}